package com.example.myaudiorecord.Myservice;

import android.app.Service;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.os.Environment;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.example.myaudiorecord.AudioRecordActivity;
import com.example.myaudiorecord.AudioRecordLruCache;
import com.example.myaudiorecord.Util.AudioRecordUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class AudioRecordService extends Service {

    private static final int RECORDER_SAMPLERATE = 44100;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

    private List<String> strings;
    private AudioRecordUtil audioRecordUtil;
    private AudioRecord recorder;
    Boolean isRecording = true;
    private int i = 1;
    private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_OUT_MONO;

    @Override
    public void onCreate() {
        super.onCreate();
        audioRecordUtil = new AudioRecordUtil();
        strings = audioRecordUtil.getListStrings();
        recorder = new AudioRecordActivity().getRecorder();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        recorder.startRecording();
        new Thread(new Runnable() {

            @Override
            public void run() {
                performLongRunningTask();
            }

            private void performLongRunningTask() {
                String filePath = new File(Environment.getExternalStorageDirectory(), "_" + System.currentTimeMillis() + "_" + "YourDirectory.pcm").getPath();
                strings.add(filePath);
                File file = new File(filePath);
                byte[] data = new byte[AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING)];
                FileOutputStream os = null;
                try {
                    os = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                int read;
                if (null != os) {
                    while (isRecording) {
                        read = recorder.read(data, 0, data.length);
                        if (AudioRecord.ERROR_INVALID_OPERATION != read) {
                            try {
                                os.write(data);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
//                //将pcm文件存入缓存区中
//                AudioRecordLruCache audioRecordLruCache = new AudioRecordLruCache();
//                audioRecordLruCache.addPcmToCache(i++, data);

            }
        }).start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isRecording = false;
        recorder.stop();
        recorder = null;
    }
}
